package com.qifan.energy.fragment;


import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.qifan.energy.activity.MyCollegeActivity;
import com.qifan.energy.activity.MyShareCodeActivity;
import com.qifan.energy.base.BaseFragment;
import com.qifan.energy.R;
import com.qifan.energy.activity.AboutActivity;
import com.qifan.energy.activity.AddressListActivity;
import com.qifan.energy.activity.BalanceActivity;
import com.qifan.energy.activity.CouponActivity;
import com.qifan.energy.activity.MyCustomerActivity;
import com.qifan.energy.activity.MyDistributeActivity;
import com.qifan.energy.activity.MyOrderActivity;
import com.qifan.energy.activity.MyRecommendActivity;
import com.qifan.energy.activity.MyServiceActivity;
import com.qifan.energy.activity.RoomEarningsActivity;
import com.qifan.energy.activity.UserInfoActivity;
import com.qifan.energy.bean.result.MemberInfo;
import com.qifan.energy.bean.result.MemberInfoResult;
import com.qifan.energy.global.GlideApp;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class MineFragment extends BaseFragment {

    @BindView(R.id.nick_tv)
    TextView nickTv;
    @BindView(R.id.avatar_iv)
    CircleImageView avatarIv;
    @BindView(R.id.balance_tv)
    TextView balanceTv;
    @BindView(R.id.coupon_card_tv)
    TextView couponCardTv;
    @BindView(R.id.game_partner_iv)
    ImageView gamePartnerIv;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment;
    }

    @Override
    protected void initView(View view) {
        /* 获取本地用户信息 */
        MemberInfo result = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        setUpUIWithModel(result);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();

        /* 获取用户信息 */
        getUserInfo();
    }

    /* 控件赋值 */
    private void setUpUIWithModel(MemberInfo result){
        if (!TextUtils.isEmpty(result.getNickname())){
            nickTv.setText(result.getNickname());
        } else {
            nickTv.setText(result.getPhone());
        }

        if (!TextUtils.isEmpty(result.getIcon())){
            GlideApp.with(getActivity())
                    .load(result.getIcon())
                    .placeholder(R.mipmap.icon_default_avatar)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.mipmap.icon_default_avatar)
                    .circleCrop()
                    .into(avatarIv);
        }
    }

    /* 获取用户信息 */
    private void getUserInfo(){
        /* 获取用户信息 */
        NetUtils.memberCenterContent(new MyObserver<MemberInfoResult>(getActivity()) {
            @Override
            public void onSuccess(MemberInfoResult result) {
                MemberInfo memberInfo = result.getMember();
                if (memberInfo != null) {
                    //保存用户信息到本地
                    SharedPreferencesUtil.putData(Keys.USER_INFO_MEMBER_KEY,memberInfo);
                    SharedPreferencesUtil.putData(Keys.USER_INFO_KEY,result);
                    //控件赋值
                    setUpUIWithModel(memberInfo);
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @OnClick(R.id.invite_iv)
    void onQuitTvClicked() {
        ToastUtils.showShort("邀请好友");
        /*Intent intent = new Intent(getActivity(),UserActivity.class);
        startActivity(intent);*/

        /*IntentUtils.startActivity(getActivity(),UserActivity.class);*/

        /*IntentUtils.startActivity(getActivity(), WebsiteActivity.class);*/
    }

    @OnClick(R.id.person_ll)
    void onTestTvClicked() {
        IntentUtils.startActivity(getActivity(), UserInfoActivity.class);
    }

    @OnClick(R.id.balance_ll)
    void onbalanceLlClicked(){
        IntentUtils.startActivity(getActivity(), BalanceActivity.class);
    }

    //优惠券
    @OnClick(R.id.coupon_card_ll)
    void onCouponCardLlClicked(){
        IntentUtils.startActivity(getActivity(), CouponActivity.class);
    }

    //我的订单
    @OnClick(R.id.my_daifukuan_tv)
    void onDaifukuanTvClicked(){
        IntentUtils.startActivity(getActivity(), MyOrderActivity.class);
    }

    @OnClick(R.id.my_daifahuo_tv)
    void onDaifahuoTvClicked(){
        IntentUtils.startActivity(getActivity(), MyOrderActivity.class);
    }

    @OnClick(R.id.my_daishouhuo_tv)
    void onDaishouhuoTvClicked(){
        IntentUtils.startActivity(getActivity(), MyOrderActivity.class);
    }

    @OnClick(R.id.my_shouhou_tv)
    void onShouhouTvClicked(){
        IntentUtils.startActivity(getActivity(), MyOrderActivity.class);
    }

    @OnClick(R.id.orderall_tv)
    void onOrderallTvClicked(){
        IntentUtils.startActivity(getActivity(), MyOrderActivity.class);
    }

    @OnClick(R.id.contacts_tv)
    void onShareTvClicked() {
        IntentUtils.startActivity(getActivity(), MyRecommendActivity.class);
        /* 获取本地用户信息 */
        /*MemberInfo result = SharedPreferencesUtil.getData(Keys.USER_INFO_MEMBER_KEY,new MemberInfo());
        if (!TextUtils.isEmpty(result.getInvitationCode())){
            UMShareUtils.shareWxLink(getActivity(), "邀请家庭成员"
                    ,String.format("快来加入我们吧，邀请码：%s","123456")
                    ,String.format(Constant.BaseUrl8085 + "?invitationCode=%s",result.getInvitationCode())
                    ,R.mipmap.ic_launcher, null);
        }*/
    }

    @OnClick(R.id.share_tv)
    void onGameTvClicked() {
        IntentUtils.startActivity(getActivity(), MyShareCodeActivity.class);
    }

    @OnClick(R.id.shop_tv)
    void onShopTvClicked() {
        IntentUtils.startActivity(getActivity(), MyDistributeActivity.class);
    }

    @OnClick(R.id.my_collect)
    void onCollectTvClicked() {
        ToastUtils.showShort("我的收藏");
    }

    @OnClick(R.id.history_tv)
    void onHistoryTvClicked(){
        ToastUtils.showShort("历史足迹");
    }

    @OnClick(R.id.address_tv)
    void onAddressTvClicked() {
        IntentUtils.startActivity(getActivity(), AddressListActivity.class);
    }

    @OnClick(R.id.earn_mgr_tv)
    void onEarnMgrTvClicked(){
        IntentUtils.startActivity(getActivity(), RoomEarningsActivity.class);
    }

    @OnClick(R.id.grades_mgr_tv)
    void onGradesMgrTvClicked(){
        ToastUtils.showShort("业绩管理");
    }

    @OnClick(R.id.my_feedback_tv)
    void onMyCustomerClicked() {
        ToastUtils.showShort("意见反馈");
        //IntentUtils.startActivity(getActivity(), MyCustomerActivity.class);
    }

    @OnClick(R.id.my_service_tv)
    void onMyServiceClicked() {
        IntentUtils.startActivity(getActivity(), MyServiceActivity.class);
    }

    @OnClick(R.id.my_college_tv)
    void onMyCollegeClicked() {
        IntentUtils.startActivity(getActivity(), MyCollegeActivity.class);
    }

    @OnClick(R.id.my_about_tv)
    void onAboutTvClicked() {
        IntentUtils.startActivity(getActivity(), AboutActivity.class);
    }
}
